package com.renren.youyou.views;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.renren.youyou.R;
import com.renren.youyou.home.HomeConfig;

public class MenuLayout extends ViewGroup implements View.OnClickListener {
	private static final String TAG = "MenuLayout";
	private ImageButton[] mMenuButtons;
	private final int kButtonWidth;
	private final int kButtonHeight;
	private OnMenuItemClickListener mOnMenuItemClickListener;

	public MenuLayout(Context context) {
		this(context, null, 0);
	}

	public MenuLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public MenuLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);

		mMenuButtons = new ImageButton[HomeConfig.TABS.length];

		for (int i = 0, length = mMenuButtons.length; i < length; i++) {
			mMenuButtons[i] = new ImageButton(context);
			mMenuButtons[i].setOnClickListener(this);
			addView(mMenuButtons[i], lp);
		}

		kButtonWidth = (int) (0.5f + getResources().getDimension(
				R.dimen.home_menu_button_width));
		kButtonHeight = (int) (0.5f + getResources().getDimension(
				R.dimen.home_menu_button_height));
	}

	public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
		mOnMenuItemClickListener = listener;
	}

	@Override
	public void onClick(View v) {
		if (mOnMenuItemClickListener != null) {
			OnMenuItemClickListener listener = mOnMenuItemClickListener;

			for (int i = 0, length = mMenuButtons.length; i < length; i++) {
				if (v.equals(mMenuButtons[i])) {
					listener.onMenuClick(i, mMenuButtons[i]);
				}
			}
		}
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		final int width = r - l;
		final int height = b - t;
		final int centerX = width / 2;
		final int evenLeft = centerX - kButtonWidth;
		int top = (height - kButtonHeight
				* (int) (mMenuButtons.length / 2f + 0.5f)) / 2;

		for (int i = 0; i < mMenuButtons.length; i += 2) {
			mMenuButtons[i]
					.layout(evenLeft, top, centerX, top += kButtonHeight);
		}

		top = (height - kButtonHeight * (int) (mMenuButtons.length / 2f + 0.5f)) / 2;

		if (mMenuButtons.length > 1) {
			for (int i = 1; i < mMenuButtons.length; i += 2) {
				mMenuButtons[i].layout(centerX, top, centerX + kButtonWidth,
						top += kButtonHeight);
			}
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		final int widthSize = widthMeasureSpec & ~(0x3 << 30);
		final int heightSize = heightMeasureSpec & ~(0x3 << 30);

		for (ImageButton button : mMenuButtons) {
			measureChild(button, kButtonWidth + MeasureSpec.EXACTLY,
					kButtonHeight + MeasureSpec.EXACTLY);
		}

		setMeasuredDimension(widthSize, heightSize);
	}

	public void setIconDrawable(int position, Drawable icon) {
		mMenuButtons[position].setImageDrawable(icon);
	}

	public static interface OnMenuItemClickListener {
		void onMenuClick(int position, View v);
	}
}
